package com.example.finalproject.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.finalproject.MainActivity;
import com.example.finalproject.R;
import com.example.finalproject.bean.LoginBean;
import com.example.finalproject.utils.DatabaseHelper;

import java.util.Map;

public class LoginActivity extends AppCompatActivity {
    private EditText editUsernameLogin, editPasswordLogin;
    private TextView textForgot;
    private Button buttonLoginPage;
    private CheckBox checkRemember;
    private DatabaseHelper databaseHelper;
    private SharedPreferences prefs;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editUsernameLogin = findViewById(R.id.editUsernameLogin);
        editPasswordLogin = findViewById(R.id.editPasswordLogin);
        buttonLoginPage = findViewById(R.id.buttonLoginPage);
        checkRemember = findViewById(R.id.checkRemember);
        textForgot = findViewById(R.id.textForgot);

        databaseHelper = DatabaseHelper.getInstance(this);

        Map<String, String> userBean = LoginBean.getLoginBean(this);
        if (userBean != null) {
            editUsernameLogin.setText(userBean.get("username"));
            editPasswordLogin.setText(userBean.get("password"));
            checkRemember.setChecked(true);
        } else {
            checkRemember.setChecked(false);
        }

        checkRemember.setOnCheckedChangeListener((view, isChecked) -> {
            String username = editUsernameLogin.getText().toString();
            String password = editPasswordLogin.getText().toString();
            if (isChecked) {
                LoginBean.saveLoginBean(this, username, password);
                Toast.makeText(this, "用户名和密码保存成功！", Toast.LENGTH_SHORT).show();
            } else {
                LoginBean.clearLoginBean(this);
                Toast.makeText(this, "已清除保存的用户名和密码！", Toast.LENGTH_SHORT).show();
            }
        });

        textForgot.setOnClickListener(view -> {
            Intent intentUpdate = new Intent(this, UpdateActivity.class);
            startActivity(intentUpdate);
            finish();
        });

        prefs = this.getSharedPreferences("userPrefs", MODE_PRIVATE);

        buttonLoginPage.setOnClickListener(view -> {
            String name = editUsernameLogin.getText().toString();
            String pass = editPasswordLogin.getText().toString();
            if (name.isEmpty() || pass.isEmpty()) {
                Toast.makeText(this, "用户名和密码不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }

            if (databaseHelper.login(name, pass)) {
                Toast.makeText(this, "登陆成功！", Toast.LENGTH_SHORT).show();

                prefs.edit().putString("loggedInUser", name).apply();

                if (checkRemember.isChecked()) {
                    LoginBean.saveLoginBean(this, name, pass);
                } else {
                    LoginBean.clearLoginBean(this);
                }

                Intent intentAccount = new Intent(this, MainActivity.class);
                intentAccount.putExtra("navigate_to_index", 2);
                startActivity(intentAccount);
                finish();
            } else {
                Toast.makeText(this, "用户名或密码错误，请重新输入!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!checkRemember.isChecked()) {
            LoginBean.clearLoginBean(this);
        }
    }
}
